02. JUnit

ND079 JPND C3 L4 A02 JUnit-

Unit Testing Frameworks

The previous manual test had many flaws:

  • Only tests one scenario
  • No boundary checking
  • Throws an exception that would interrupt other tests
  • Must be manually executed

Unit Testing Frameworks are designed to solve some of these problems.

JUnit

JUnit is the main testing framework for Java. It provides tools for writing unit tests and automatically executing them. It has built-in support from Maven and IDEs like IntelliJ.

Adding JUnit as a Maven Dependency

Adding JUnit to your project is as easy as including the dependency. Your Maven quickstart project probably included a JUnit 4 dependency, but we will be using this one.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

You can also find the latest dependency information at https://junit.org

Creating a Test Class

Test Class: Class containing all the unit tests for a corresponding application class.

  • Location: src/test/java
  • Package: The same as your application class.

For example:

  • Application Class: src/main/java/com.udacity.jpnd.App
  • Test Class: src/test/java/com.udacity.jpnd.AppTest